#coding:utf-8
#from django.test import TestCase
from unittest import TestCase
from major.models import Major, Char
import datetime, time

class MajorTest(TestCase):
    def setUp(self):
        #public var
        self.author_id = 1
        self.name = "_"
        self.birthday = int(datetime.date.today().isoformat().replace("-", ""))
        self.birthtime = int(time.time())
        self.is_del = False
        #create
        item = Major.objects.create(author_id=self.author_id,
                                    is_del=self.is_del,
                                    birthtime=self.birthtime,
                                    birthday=self.birthday,
                                    name=self.name)
        self.pk = item.pk
        #for tear down
        self.del_list = [item.pk]
    def tearDown(self):
        Major.objects.filter(pk__in=self.del_list).delete()
    #testcase
    def test_create(self):
        a_name = u"测试用，专业01"
        author_id = 1
        #class var
        a_time = int(time.time())
        a_date = int(datetime.date.today().isoformat().replace("-", ""))
        a_item = Major.create({
            "author_id": 1,
            "is_del": True,
            "birthtime": 1,
            "birthday": 1,
            "name": a_name,
        })
        #for tear down
        self.del_list.append(a_item.pk)
        item = Major.objects.get(pk=a_item.pk)
        #only "author_id", and "name" could be definded
        self.assertEqual(item.name, a_name)
        self.assertEqual(item.author_id, author_id)
        self.assertNotEqual(item.birthtime, 1)
        self.assertNotEqual(item.birthday, 1)
        self.assertNotEqual(item.is_del, True)
        #other default values
        #self.assertEqual(item.birthtime, a_time)
        self.assertEqual(item.birthday, a_date)
        self.assertEqual(item.is_del, False)
        #not null
        except_str = ""
        try:
            a_item = Major.create({
                "name": a_name,
            })
            self.del_list.append(a_item.pk)
        except Exception as ex:
            except_str = str(ex)
        self.assertNotEqual(except_str, "")        
    def test_create_rf(self):
        response_format = Major.create_rf(
            author_id=1,
            name="a_name",
        )
        self.del_list.append(response_format["data"][0]["pk"])
        self.assertEqual(response_format["value"], True)
        self.assertEqual(response_format["desp"], "")
        self.assertEqual(len(response_format["data"]), 1)
        result_data = response_format["data"][0]
        self.assertIn("pk", result_data)
        self.assertIn("author_id", result_data)
        self.assertEqual(result_data["author_id"], 1)
        self.assertIn("name", result_data)
        self.assertEqual(result_data["name"], "a_name")
        self.assertIn("birthtime", result_data)
        self.assertIn("birthday", result_data)    
    def test_del(self):
        del_return = Major.dele(self.pk)
        item = Major.objects.filter(pk=self.pk)
        #item exist
        self.assertEqual(item.count(), 1)
        self.assertEqual(item[0].is_del, True)
        #return value is a num, how much item is set to dele status
        self.assertEqual(del_return, 1)
        #can not be searched
        search_list = Major.search({"pk": self.pk})
        self.assertEqual(search_list.count(), 0)
    def test_update(self):
        a_name = u"测试用，专业01经过更新"
        Major.update(self.pk, {
            "author_id": 2,
            "is_del": True,
            "birthtime": 1,
            "birthday": 1,
            "name": a_name,
        })
        item = Major.objects.get(pk=self.pk)
        #only "name" and "id_del" could be changed
        self.assertEqual(item.author_id, 1)
        self.assertEqual(item.name, a_name)
        #others do not change
        self.assertEqual(item.birthtime, self.birthtime)
        self.assertEqual(item.birthday, self.birthday)
        self.assertEqual(item.is_del, True)
    def test_search(self):
        item1 = Major.create({
            "author_id": self.author_id,
            "name": "name_in_major",
        })
        self.del_list.append(item1.pk)
        item2 = Major.create({
            "author_id": self.author_id,
            "name": "name_in_major",
        })
        self.del_list.append(item2.pk)
        #"del" and "test" status can not be searched
        Major.dele(self.pk)
        search_result = Major.search({"pk": self.pk})
        self.assertEqual(search_result.count(), 0)
        #pk
        search_result = Major.search({"pk": item1.pk})
        self.assertEqual(search_result.count(), 1)
        search_result = Major.search({"pk": 0})
        self.assertEqual(search_result.count(), 0)
        #pk list
        search_result = Major.search({"pk_list": [item2.pk, item1.pk]})
        self.assertEqual(search_result.count(), 2)
        #birthtime
        search_result = Major.search({"birthtime": item1.birthtime})
        self.assertGreaterEqual(search_result.count(), 1)
        search_result = Major.search({"birthtime": 1})
        self.assertEqual(search_result.count(), 0)
        #birthday
        search_result = Major.search({"birthday": item1.birthday})
        self.assertEqual(search_result.count(), 2)
        search_result = Major.search({"birthday": 1})
        self.assertEqual(search_result.count(), 0)
        #name
        search_result = Major.search({"name": "in"})
        self.assertEqual(search_result.count(), 2)
        search_result = Major.search({"name": "out"})
        self.assertEqual(search_result.count(), 0)
        # and search field
        search_result = Major.search({"pk": item1.pk,
                                      "name": "major",
                                      "birthtime": item1.birthtime,
                                      "birthday": item1.birthday,})
        self.assertEqual(search_result.count(), 1)
    def test_del_rf(self):
        response_format = Major.dele_rf(self.pk)
        self.assertEqual(response_format["value"], True)
        self.assertEqual(response_format["desp"], "")
        self.assertEqual(response_format["data"], [])
        response_format = Major.dele_rf(0)
        self.assertEqual(response_format["value"], False)
        self.assertEqual(response_format["desp"], "No such item.")
        self.assertEqual(response_format["data"], [])
    def test_update_rf(self):
        item = Major.create({
            "author_id": self.author_id,
            "name": "name_in_major",
        })
        self.del_list.append(item.id)
        response_format = Major.update_rf(item.pk,
                                          author_id = 2,
                                          name = "a_name",)
        self.assertEqual(response_format["value"], True)
        self.assertEqual(response_format["desp"], "")
        result_data = response_format["data"][0]
        self.assertEqual(result_data["pk"], item.pk)
        self.assertEqual(result_data["author_id"], self.author_id)
        self.assertEqual(result_data["name"], "a_name")
        self.assertIn("birthtime", result_data)
        self.assertIn("birthday", result_data)
        response_format = Major.update_rf(0,
                                          author_id = 2,
                                          name = "a_name",)
        self.assertEqual(response_format["value"], False)
        self.assertEqual(response_format["desp"], "No such item.")
        self.assertEqual(response_format["data"], [])
    def test_search_rf(self):
        item1 = Major.create({
            "author_id": self.author_id,
            "name": "name_in_major",
        })
        self.del_list.append(item1.pk)
        item2 = Major.create({
            "author_id": self.author_id,
            "name": "name_in_major",
        })
        self.del_list.append(item2.pk)
        #search
        response_format = Major.search_rf(pk_list=[item2.pk, item1.pk])
        self.assertEqual(response_format["value"], True)
        self.assertEqual(response_format["desp"], "")
        #data
        self.assertEqual(len(response_format["data"]), 2)
        result_data1 = response_format["data"][0]
        result_data2 = response_format["data"][1]
        #1
        self.assertEqual(result_data1["pk"], item1.pk)
        self.assertEqual(result_data1["author_id"], self.author_id)
        self.assertEqual(result_data1["name"], "name_in_major")
        self.assertIn("birthtime", result_data1)
        self.assertIn("birthday", result_data1)
        #2
        self.assertEqual(result_data2["pk"], item2.pk)
        self.assertEqual(result_data2["author_id"], self.author_id)
        self.assertEqual(result_data2["name"], "name_in_major")
        self.assertIn("birthtime", result_data2)
        self.assertIn("birthday", result_data2)
        #faile
        response_format = Major.search_rf(pk=0)
        self.assertEqual(response_format["value"], True)
        self.assertEqual(response_format["desp"], "") 
        self.assertEqual(len(response_format["data"]), 0)
1
class CharTest(TestCase):
    def createChar(self):
        #creat char
        self.char1 = Char.create({"author_id": self.author_id,
                                  "major_id": self.major_pk,
                                  "name": "char1",
                                  "index": 1})
        self.char_del_list.append(self.char1.pk)
        self.char1_01 = Char.create({"author_id": self.author_id,
                                "major_id": self.major_pk,
                                "name": "char1_01",
                                "parent_id": self.char1.pk,
                                "index": 1})
        self.char_del_list.append(self.char1_01.pk)        
        self.char1_02 = Char.create({"author_id": self.author_id,
                                "major_id": self.major_pk,
                                "name": "char1_02",
                                "parent_id": self.char1.pk,
                                "index": 2})
        self.char_del_list.append(self.char1_02.pk) 
        self.char1_03 = Char.create({"author_id": self.author_id,
                                "major_id": self.major_pk,
                                "name": "char1_03",
                                "parent_id": self.char1.pk,
                                "index": 3})
        self.char_del_list.append(self.char1_03.pk) 
        self.char1_04 = Char.create({"author_id": self.author_id,
                                "major_id": self.major_pk,
                                "name": "char1_04",
                                "parent_id": self.char1.pk,
                                "index": 4})
        self.char_del_list.append(self.char1_04.pk)
        self.char2 = Char.create({"author_id": self.author_id,
                                  "major_id": self.major_pk,
                                  "name": "char2",
                                  "index": 2})
        self.char_del_list.append(self.char2.pk)
        self.char2_01 = Char.create({"author_id": self.author_id,
                                "major_id": self.major_pk,
                                "name": "char2_01",
                                "parent_id": self.char2.pk,
                                "index": 1})
        self.char_del_list.append(self.char2_01.pk) 
        self.char2_02 = Char.create({"author_id": self.author_id,
                                "major_id": self.major_pk,
                                "name": "char2_02",
                                "parent_id": self.char2.pk,
                                "index": 2})
        self.char_del_list.append(self.char2_02.pk) 
        self.char2_03 = Char.create({"author_id": self.author_id,
                                "major_id": self.major_pk,
                                "name": "char2_03",
                                "parent_id": self.char2.pk,
                                "index": 3})
        self.char_del_list.append(self.char2_03.pk) 
        self.char2_04 = Char.create({"author_id": self.author_id,
                                "major_id": self.major_pk,
                                "name": "char2_04",
                                "parent_id": self.char2.pk,
                                "index": 4})   
        self.char_del_list.append(self.char2_04.pk)
        self.char3 = Char.create({"author_id": self.author_id,
                                  "major_id": self.major_pk,
                                  "name": "char3",
                                  "index": 3})
        self.char_del_list.append(self.char3.pk)
        self.char3_01 = Char.create({"author_id": self.author_id,
                                "major_id": self.major_pk,
                                "name": "char3_01",
                                "parent_id": self.char3.pk,
                                "index": 1})
        self.char_del_list.append(self.char3_01.pk) 
        self.char3_02 = Char.create({"author_id": self.author_id,
                                "major_id": self.major_pk,
                                "name": "char3_02",
                                "parent_id": self.char3.pk,
                                "index": 2})
        self.char_del_list.append(self.char3_02.pk) 
        self.char3_03 = Char.create({"author_id": self.author_id,
                                "major_id": self.major_pk,
                                "name": "char3_03",
                                "parent_id": self.char3.pk,
                                "index": 3})
        self.char_del_list.append(self.char3_03.pk) 
        self.char3_04 = Char.create({"author_id": self.author_id,
                                "major_id": self.major_pk,
                                "name": "char3_04",
                                "parent_id": self.char3.pk,
                                "index": 4})
        self.char_del_list.append(self.char3_04.pk)
        self.char4 = Char.create({"author_id": self.author_id,
                                  "major_id": self.major_pk,
                                  "name": "char4",
                                  "index": 4})
        self.char_del_list.append(self.char4.pk)
        self.char4_01 = Char.create({"author_id": self.author_id,
                                "major_id": self.major_pk,
                                "name": "char4_01",
                                "parent_id": self.char4.pk,
                                "index": 1})
        self.char_del_list.append(self.char4_01.pk) 
        self.char4_02 = Char.create({"author_id": self.author_id,
                                "major_id": self.major_pk,
                                "name": "char4_02",
                                "parent_id": self.char4.pk,
                                "index": 2})
        self.char_del_list.append(self.char4_02.pk) 
        self.char4_03 = Char.create({"author_id": self.author_id,
                                "major_id": self.major_pk,
                                "name": "char4_03",
                                "parent_id": self.char4.pk,
                                "index": 3})
        self.char_del_list.append(self.char4_03.pk) 
        self.char4_04 = Char.create({"author_id": self.author_id,
                                "major_id": self.major_pk,
                                "name": "char4_04",
                                "parent_id": self.char4.pk,
                                "index": 4})   
        self.char_del_list.append(self.char4_04.pk)        
    def setUp(self):
        #public var
        self.author_id = 1
        self.major_del_list = []
        self.char_del_list = []
        #create major
        item = Major.create({
            "author_id": self.author_id,
            "name": "_",
        })
        self.major_pk = item.pk
        self.major_del_list.append(item.pk)
    def tearDown(self):
        Major.objects.filter(pk__in = self.major_del_list).delete()
        Char.objects.filter(pk__in = self.char_del_list).delete()
        pass
    def test_create(self):
        #set up
        char = Char.create({"author_id": self.author_id,
                            "name": "char1",
                            "major_id": self.major_pk})
        self.char_del_list.append(char.pk)
        char2 = Char.create({"author_id": self.author_id,
                             "name": "char2",
                             "major_id": self.major_pk,
                             "parent_id": char.pk,
                             "is_kp": True,
                             "index": 2})
        self.char_del_list.append(char2.pk)
        #test
        self.assertIsNotNone(char.pk)
        self.assertEqual(char.author_id, self.author_id)
        self.assertEqual(char.name, "char1")
        self.assertEqual(char.major_id, self.major_pk)
        #default value
        self.assertEqual(char.parent_id, None)
        self.assertEqual(char.is_kp, False)
        self.assertEqual(char.index, 1)
        #changed default value
        self.assertEqual(char2.parent_id, char.pk)
        self.assertEqual(char2.is_kp, True)
        self.assertEqual(char2.index, 2)
        #not null
        exception_str = ""
        try:
            char3 = Char.create({"author_id": self.author_id,
                                 "name": "char3"})
            self.char_del_list.append(char3.pk)
        except Exception as ex:
            exception_str = str(ex)
        finally:
            self.assertNotEqual(exception_str, "")
    def test_create_rf(self):
        response_format = Char.create_rf(author_id=self.author_id,
                                         name="char2",
                                         major_id=self.major_pk)
        self.char_del_list.append(response_format["data"][0]["pk"])
        self.assertEqual(response_format["value"], True)
        self.assertEqual(response_format["desp"], "")
        self.assertEqual(len(response_format["data"]), 1)
        result_data = response_format["data"][0]
        self.assertIn("pk", result_data)
        self.assertIn("author_id", result_data)
        self.assertIn("name", result_data)
        self.assertIn("major_id", result_data)
        self.assertIn("parent_id", result_data)
        self.assertIn("is_kp", result_data)
        self.assertIn("index", result_data)
        self.assertEqual(result_data["author_id"], self.author_id)
        self.assertEqual(result_data["name"], "char2")
        self.assertEqual(result_data["major_id"], self.major_pk)
        self.assertEqual(result_data["parent_id"], None)
        self.assertEqual(result_data["is_kp"], False)
        self.assertEqual(result_data["index"], 1)
    def test_update_index_first_not_move(self):
        self.createChar()
        #parent id, num
        Char.update(self.char1_01.pk, {"index": 1})
        item = Char.objects.get(pk=self.char1_01.pk)
        item2 = Char.objects.get(pk=self.char1_02.pk)
        item3 = Char.objects.get(pk=self.char1_03.pk)
        item4 = Char.objects.get(pk=self.char1_04.pk)
        self.assertEqual(item.index, 1)
        self.assertEqual(item2.index, 2)
        self.assertEqual(item3.index, 3)
        self.assertEqual(item4.index, 4)
        #parent id, none
        Char.update(self.char1.pk, {"index": 1})
        item = Char.objects.get(pk=self.char1.pk)
        item2 = Char.objects.get(pk=self.char2.pk)
        item3 = Char.objects.get(pk=self.char3.pk)
        item4 = Char.objects.get(pk=self.char4.pk)
        self.assertEqual(item.index, 1)
        self.assertEqual(item2.index, 2)
        self.assertEqual(item3.index, 3)
        self.assertEqual(item4.index, 4)
    def test_update_index_first_neighbour(self):
        self.createChar()
        #
        Char.update(self.char1_01.pk, {"index": 2})
        item = Char.objects.get(pk=self.char1_01.pk)
        item2 = Char.objects.get(pk=self.char1_02.pk)
        item3 = Char.objects.get(pk=self.char1_03.pk)
        item4 = Char.objects.get(pk=self.char1_04.pk)
        self.assertEqual(item2.index, 1)
        self.assertEqual(item.index, 2)
        self.assertEqual(item3.index, 3)
        self.assertEqual(item4.index, 4)
        #parent id, none
        Char.update(self.char1.pk, {"index": 2})
        item = Char.objects.get(pk=self.char1.pk)
        item2 = Char.objects.get(pk=self.char2.pk)
        item3 = Char.objects.get(pk=self.char3.pk)
        item4 = Char.objects.get(pk=self.char4.pk)
        self.assertEqual(item2.index, 1)
        self.assertEqual(item.index, 2)
        self.assertEqual(item3.index, 3)
        self.assertEqual(item4.index, 4)
    def test_update_index_first_last(self):
        self.createChar()
        #
        Char.update(self.char1_01.pk, {"index": 4})
        item = Char.objects.get(pk=self.char1_01.pk)
        item2 = Char.objects.get(pk=self.char1_02.pk)
        item3 = Char.objects.get(pk=self.char1_03.pk)
        item4 = Char.objects.get(pk=self.char1_04.pk)
        self.assertEqual(item2.index, 1)
        self.assertEqual(item3.index, 2)
        self.assertEqual(item4.index, 3)
        self.assertEqual(item.index, 4)
        #parent id, none
        Char.update(self.char1.pk, {"index": 4})
        item = Char.objects.get(pk=self.char1.pk)
        item2 = Char.objects.get(pk=self.char2.pk)
        item3 = Char.objects.get(pk=self.char3.pk)
        item4 = Char.objects.get(pk=self.char4.pk)
        self.assertEqual(item2.index, 1)
        self.assertEqual(item3.index, 2)
        self.assertEqual(item4.index, 3)
        self.assertEqual(item.index, 4)
    def test_update_index_last_not_move(self):
        self.createChar()
        #
        Char.update(self.char1_04.pk, {"index": 4})
        item = Char.objects.get(pk=self.char1_01.pk)
        item2 = Char.objects.get(pk=self.char1_02.pk)
        item3 = Char.objects.get(pk=self.char1_03.pk)
        item4 = Char.objects.get(pk=self.char1_04.pk)
        self.assertEqual(item.index, 1)
        self.assertEqual(item2.index, 2)
        self.assertEqual(item3.index, 3)
        self.assertEqual(item4.index, 4)
        #parent id, none
        Char.update(self.char4.pk, {"index": 4})
        item = Char.objects.get(pk=self.char1.pk)
        item2 = Char.objects.get(pk=self.char2.pk)
        item3 = Char.objects.get(pk=self.char3.pk)
        item4 = Char.objects.get(pk=self.char4.pk)
        self.assertEqual(item.index, 1)
        self.assertEqual(item2.index, 2)
        self.assertEqual(item3.index, 3)
        self.assertEqual(item4.index, 4)
    def test_update_index_last_neighbour(self):
        self.createChar()
        #
        Char.update(self.char1_04.pk, {"index": 3})
        item = Char.objects.get(pk=self.char1_01.pk)
        item2 = Char.objects.get(pk=self.char1_02.pk)
        item3 = Char.objects.get(pk=self.char1_03.pk)
        item4 = Char.objects.get(pk=self.char1_04.pk)
        self.assertEqual(item.index, 1)
        self.assertEqual(item2.index, 2)
        self.assertEqual(item4.index, 3)
        self.assertEqual(item3.index, 4)
        #parent id, none
        Char.update(self.char4.pk, {"index": 3})
        item = Char.objects.get(pk=self.char1.pk)
        item2 = Char.objects.get(pk=self.char2.pk)
        item3 = Char.objects.get(pk=self.char3.pk)
        item4 = Char.objects.get(pk=self.char4.pk)
        self.assertEqual(item.index, 1)
        self.assertEqual(item2.index, 2)
        self.assertEqual(item4.index, 3)
        self.assertEqual(item3.index, 4)
    def test_update_index_last_first(self):
        self.createChar()
        #
        Char.update(self.char1_04.pk, {"index": 1}) 
        item = Char.objects.get(pk=self.char1_01.pk)
        item2 = Char.objects.get(pk=self.char1_02.pk)
        item3 = Char.objects.get(pk=self.char1_03.pk)
        item4 = Char.objects.get(pk=self.char1_04.pk)
        self.assertEqual(item4.index, 1)
        self.assertEqual(item.index, 2)
        self.assertEqual(item2.index, 3)
        self.assertEqual(item3.index, 4)
        #parent id, none
        Char.update(self.char4.pk, {"index": 1})
        item = Char.objects.get(pk=self.char1.pk)
        item2 = Char.objects.get(pk=self.char2.pk)
        item3 = Char.objects.get(pk=self.char3.pk)
        item4 = Char.objects.get(pk=self.char4.pk)
        self.assertEqual(item4.index, 1)
        self.assertEqual(item.index, 2)
        self.assertEqual(item2.index, 3)
        self.assertEqual(item3.index, 4)
    def test_update_father_id_none(self):
        self.createChar()
        Char.update(self.char1.pk, {"parent_id": None})
        Char.update(self.char1_01.pk, {"parent_id": 0})
        item = Char.objects.get(pk=self.char1.pk)
        item2 = Char.objects.get(pk=self.char1_01.pk)
        self.assertEqual(item.parent_id, None)
        self.assertEqual(item2.parent_id, None)
    def test_update_father_id_not_none(self):
        self.createChar()
        Char.update(self.char1.pk, {"parent_id": self.char2.pk})
        Char.update(self.char1_01.pk, {"parent_id": self.char2.pk})
        Char.update(self.char2_01.pk, {"parent_id": self.char1.pk})
        item = Char.objects.get(pk=self.char1.pk)
        item2 = Char.objects.get(pk=self.char1_01.pk)
        item3 = Char.objects.get(pk=self.char2_01.pk)
        self.assertEqual(item.parent_id, self.char2.pk)
        self.assertEqual(item2.parent_id, self.char2.pk)
        self.assertEqual(item3.parent_id, self.char1.pk)
    def test_update_father_id_mix_index_root_to_not(self):
        self.createChar()
        #root to not root
        Char.update(self.char1.pk, {"parent_id": self.char2.pk, "index": 1})
        item = Char.objects.get(pk=self.char1.pk)
        item2 = Char.objects.get(pk=self.char2.pk)
        item3 = Char.objects.get(pk=self.char3.pk)
        item4 = Char.objects.get(pk=self.char4.pk)
        self.assertEqual(item.parent_id, self.char2.pk)
        self.assertEqual(item.index, 1)
        self.assertEqual(item2.index, 1)
        self.assertEqual(item3.index, 2)
        self.assertEqual(item4.index, 3)
        item = Char.objects.get(pk=self.char2_01.pk)
        item2 = Char.objects.get(pk=self.char2_02.pk)
        item3 = Char.objects.get(pk=self.char2_03.pk)
        item4 = Char.objects.get(pk=self.char2_04.pk)
        self.assertEqual(item.index, 2)
        self.assertEqual(item2.index, 3)
        self.assertEqual(item3.index, 4)
        self.assertEqual(item4.index, 5)
    def test_update_father_id_mix_index_to_root(self):
        self.createChar()
        #not root to root
        Char.update(self.char2_01.pk, {"parent_id": 0, "index": 1})
        item = Char.objects.get(pk=self.char1.pk)
        item2 = Char.objects.get(pk=self.char2.pk)
        item3 = Char.objects.get(pk=self.char3.pk)
        item4 = Char.objects.get(pk=self.char4.pk)
        item5 = Char.objects.get(pk=self.char2_01.pk)
        self.assertEqual(item5.parent_id, None)
        self.assertEqual(item5.index, 1)
        self.assertEqual(item.index, 2)
        self.assertEqual(item2.index, 3)
        self.assertEqual(item3.index, 4)
        self.assertEqual(item4.index, 5)
        item = Char.objects.get(pk=self.char2_02.pk)
        item2 = Char.objects.get(pk=self.char2_03.pk)
        item3 = Char.objects.get(pk=self.char2_04.pk)
        self.assertEqual(item.index, 1)
        self.assertEqual(item2.index, 2)
        self.assertEqual(item3.index, 3)


"""import unittest
from coverage import coverage

class CoverageTest(TestCase):
    def test_do_coverage(self):
        cov = coverage()
        cov.use_cache(1)    
        cov.start()
        test_loader = unittest.TestLoader()
        test_suite =  unittest.TestSuite()
        #suite.loadTestsFromModule(major.tests)
        test_suite.addTests(test_loader.loadTestsFromTestCase(MajorTest))
        test_suite.addTests(test_loader.loadTestsFromTestCase(CharTest))
        #unittest.TextTestRunner(verbosity=2).run(test_suite)
        test_result = unittest.TestResult()
        test_suite.run(test_result)
        cov.stop()
        cov.html_report(directory='covhtml')"""




def for_test_init_useless():
    major_list = ["测试课程01", "测试课程02", "测试课程03", "测试课程04"]
    char_1_list = ["第一章", "第二章", "第三章", "第四章"]
    char_2_list = ["第一节", "第二节", "第三节", "第四节"]
    kp_list = ["知识点01", "知识点02", "知识点03", "知识点04"]
    for major_index in range(4):
        major = Major.create({"author_id" : 1, "name": major_list[major_index]})
        for index1 in range(4):
            char_1 = Char.create({"author_id" : 1, "major_id":major.pk, "name": char_1_list[index1],
                                  "index": index1+1})
            for index2 in range(4):
                char_2 = Char.create({"author_id" : 1, "major_id":major.pk, "name": char_2_list[index2],
                                      "index": index2+1, "parent_id": char_1.pk})
                for kp_index in range(4):
                    kp = Char.create({"author_id" : 1, "major_id":major.pk, "name": kp_list[kp_index],
                                      "index": kp_index+1, "parent_id": char_2.pk, "is_kp": True})
